package idoc.index;

import idoc.index.DataBlockLink.DataBlock;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class FileMap {
	
	
	private   int mapSize=100000000;
	private RandomAccessFile file ;
	private DataBlockLink link ;
	public FileMap() throws FileNotFoundException{
		file = new RandomAccessFile("1.dat", "rw");
		link = new DataBlockLink("a.dat", 512, 16);
	}
	
	Random r = new Random();
	public void put(Long key,Long val) throws IOException{
		
		long pp = r.nextLong();
		long p = pointerFor(key);
		file.seek(p);
		if(p>file.length()){
			file.writeLong(pp);
		}else{
			long pp1 = file.readLong();
			DataBlock block =null;
			if(pp1>0){
				block =link.get(pp);
			}else{
				block =link.create();
				file.writeLong(block.pointer());
			}
			block.writeLong(val);
		}
		
	}
	
	public Long[] get(long key) throws IOException{
		
		file.seek(pointerFor(key));
		long pp = file.readLong();
		List<Long> ls = new ArrayList<Long>();
		if(pp>0){
			// get data block and read data from 
			DataBlock block = link.get(pp);
			while(true){
				Long l = block.readLong();
				
			}
			//return  Long Array;
		}
		return null;
	}
	
	private long pointerFor(long key){
		
		long idx = (key%mapSize)*8;
		return idx;
		
	}
}
